package org.fing.edu.uy.admin.managedBean;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.fing.edu.uy.admin.config.MgrAdminConfig;
import org.fing.edu.uy.admin.utils.FacesUtils;

@SessionScoped
@ManagedBean(name="loginController")
public class LoginController implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private String username;
	private String password;
	
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public String checkAccess(){
		String user = MgrAdminConfig.getInstance().getSecureProperty("username", "admin");
		String pass = MgrAdminConfig.getInstance().getSecureProperty("password", "admin");
		if(username.equals(user) && password.equals(pass)){
			setLogin();
			return "success";
		} else {
			displayLoginError();
			return "fail";
		}
	}
	
	private void displayLoginError(){
		FacesUtils.addSev("Invalid username or password, verify the data and try again.");
	}
	
	private void setLogin(){
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.setAttribute("login", true);
	}

}
